﻿using UnityEngine;
using System.Collections;

public enum SOCIALMENUTYPE
{
    NONE,
    MAILBOX=0,
    GIFT,
    FRIENDLIST,
    ADDFRIEND,
    RECOMMANDFRIEND
};

public class SocialMenuManager : LTEventListenerC {
    public static SocialMenuManager singleton;

    [HideInInspector]
    //temporary setup
    public SOCIALMENUTYPE currentSocialMenuType = SOCIALMENUTYPE.FRIENDLIST;

    public AddSocialMenuItems itemGrid;

    public GameObject prevTabObject;

    public TopInfoBar topInfoBar;

    public GameObject AddFriendObject;

    public UILabel friendNameForSearch;
	
	public UILabel mailReceiverName;
	public UILabel mailBody;

	public GameObject[] tabObjectList;

    void Awake()
    {
        if (singleton == null)
            singleton = this;
    }

	// Use this for initialization
	void Start () {
        currentSocialMenuType = SOCIALMENUTYPE.FRIENDLIST;
		Vector3 pos = tabObjectList[(int)currentSocialMenuType].transform.localPosition;
		pos.y += 5f;
		tabObjectList[(int)currentSocialMenuType].transform.localPosition = pos;
		prevTabObject = tabObjectList[(int)currentSocialMenuType];
        //temporary 
        LoadingWindow.window.ShowShortLoading();
        RestAPI.singleton.GetMyFriendList(ReturnCallback);
	}
	
	// Update is called once per frame
	void Update () {
        
	}

    void TabClicked(GameObject tabObject)
    {
		if( prevTabObject != null )
		{
//        prevTabObject.GetComponent<UISprite>().spriteName = "ui_shop_tab_b";
//			prevTabObject.GetComponentInChildren<UISprite>().alpha = 0.7f;
//			Color prevColor =  prevTabObject.GetComponent<UIButton>().defaultColor;
//			prevColor.a = 0.7f;
//			prevTabObject.GetComponent<UIButton>().defaultColor = prevColor;
			Vector3 prevPos = prevTabObject.transform.localPosition;
			prevPos.y -= 5f;
			prevTabObject.transform.localPosition = prevPos;
//        prevTabObject.GetComponent<UISprite>().depth = 1;
		}
//        tabObject.GetComponent<UISprite>().spriteName = "ui_shop_tab_a";
//		tabObject.GetComponentInChildren<UISprite>().alpha = 1f;
//		Color curColor =  tabObject.GetComponent<UIButton>().defaultColor;
//		curColor.a = 1f;
//		tabObject.GetComponent<UIButton>().defaultColor = curColor;
		Vector3 curPos = tabObject.transform.localPosition;
		curPos.y += 5f;
		tabObject.transform.localPosition = curPos;
//        tabObject.GetComponent<UISprite>().depth = 3;
        prevTabObject = tabObject;
        Debug.Log(tabObject.name);
        if (tabObject.name.Contains("Button_1") == true)
        {
            currentSocialMenuType = SOCIALMENUTYPE.MAILBOX;
            LoadingWindow.window.ShowShortLoading();
            RestAPI.singleton.GetMyFriendRequestList(ReturnCallback);
            if (AddFriendObject.activeSelf)
                AddFriendObject.SetActive(false);
        }
        else if (tabObject.name.Contains("Button_2") == true)
        {
            currentSocialMenuType = SOCIALMENUTYPE.GIFT;
            if (AddFriendObject.activeSelf)
                AddFriendObject.SetActive(false);
			itemGrid.ClearItems();
        }
        else if (tabObject.name.Contains("Button_3") == true)
        {
            currentSocialMenuType = SOCIALMENUTYPE.FRIENDLIST;
            LoadingWindow.window.ShowShortLoading();
            RestAPI.singleton.GetMyFriendList(ReturnCallback);
            if (AddFriendObject.activeSelf)
                AddFriendObject.SetActive(false);
        }
        else if (tabObject.name.Contains("Button_4") == true)
        {
            currentSocialMenuType = SOCIALMENUTYPE.ADDFRIEND;
            AddFriendObject.SetActive(true);
            itemGrid.SetItems(SOCIALMENUTYPE.ADDFRIEND);
        }
        else if (tabObject.name.Contains("Button_5") == true)
        {
            currentSocialMenuType = SOCIALMENUTYPE.RECOMMANDFRIEND;
            if (AddFriendObject.activeSelf)
                AddFriendObject.SetActive(false);
			itemGrid.ClearItems();
        }
    }
	
    //int currentMailBoxPageIndex = 1;
    void ReturnCallback(Hashtable returnData)
    {
        LoadingWindow.window.HideLoading();
        switch ((RestAPIProtocol)returnData["protocol"])
        {
            case RestAPIProtocol.GETMYFRIENDLIST:
                //RestAPI.singleton.GetMyMailboxList(ReturnCallback, currentMailBoxPageIndex++);
				itemGrid.SetItems(currentSocialMenuType);
                break;
            case RestAPIProtocol.SENDFRIENDREQUEST:
				friendNameForSearch.text = "";
                break;
            case RestAPIProtocol.GETMYFRIENDREQUESTLIST:
                itemGrid.SetItems(currentSocialMenuType);
                break;
			case RestAPIProtocol.GETMYMAILBOXLIST:
				itemGrid.SetItems(currentSocialMenuType);
				break;
			case RestAPIProtocol.SENDMAIL:
				mailReceiverName.text = "";
				mailBody.text = "";
				break;
        }
    }

    void OnSubmitFriendRequest()
    {
		string friendName = friendNameForSearch.text;
		char[] trimChar = { '|', '\r' };
		friendName = friendName.TrimEnd(trimChar);
        RestAPI.singleton.SendFriendRequest(ReturnCallback, friendName);
    }
	
	void OnSubmitSendMessge()
	{
		RestAPI.singleton.SendMail(ReturnCallback, mailReceiverName.text, mailBody.text);
	}

    public override void OnEvent(int EventId)
    {
        if (EventId == 0)
        {

        }
    }
}
